/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main_com.Controlers.friend;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import main_com.mapper.FriendMapper;
import main_com.pojo.LoginBean;
import main_com.pojo.LookFriendBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/AddFriendController")
public class AddFriendController {
    //    public void wrong1(){
//        String msg="不允许有空，添加失败！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void wrong2(){
//        String msg="用户名已存在，添加失败！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void right(){
//        String msg="填写信息合格，添加成功！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
    @Autowired
    FriendMapper friendMapper;

    @RequestMapping("/addFriend")
    protected void addFriend(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String workPlace = request.getParameter("workPlace");
        String place = request.getParameter("place");
        String QQ = request.getParameter("QQ");
        if (name.length() == 0 || phone.length() == 0 || email.length() == 0 || workPlace.length() == 0 || QQ.length() == 0) {
//            wrong1();
            response.sendRedirect("http://localhost/friendManager/addFriend.jsp");
        } else {
            try {
                String userName = "";
                HttpSession session = request.getSession();
                ArrayList login = (ArrayList) session.getAttribute("login");
                if (login == null || login.isEmpty()) {
                    response.sendRedirect("http://localhost/login.jsp");
                } else {
                    for (int i = login.size() - 1; i >= 0; i--) {
                        LoginBean nn = (LoginBean) login.get(i);
                        userName = nn.getUserName();
                    }
                }
                int k;
                k = friendMapper.findFriendByNameAndUserName(name, userName);
                if (k > 0) {
//                    wrong2();
                    response.sendRedirect("http://localhost/friendManager/addFriend.jsp");
                } else {
                    friendMapper.addFriend(new LookFriendBean(userName, name, phone, email, workPlace, place, QQ));
                }
                // 获取当前页数，默认是第1页
                String pageParam = request.getParameter("page");
                int currentPage = (pageParam != null) ? Integer.parseInt(pageParam) : 1;
                int pageSize = 3; // 每页显示3条数据
                int offset = (currentPage - 1) * pageSize;

                // 获取总记录数
                int totalCount = friendMapper.getTotalFriendCount(userName);
                int totalPages = (int) Math.ceil((double) totalCount / pageSize);

                // 将数据和分页信息存入 session
                session.setAttribute("currentPage", currentPage);
                session.setAttribute("totalPages", totalPages);
                ArrayList<LookFriendBean> friendslist = friendMapper.getFriendByUserNameWithPagination(userName, offset, pageSize);
                session.setAttribute("friendslist", friendslist);
                response.sendRedirect("http://localhost/friendManager/lookFriend.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}